﻿this.screenshotPreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<div id='preview'><img src='" + this.rel + "' height='300px' />"+ c +"</div>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


$(document).ready(function(){
    $("div#debug_console h1").click(function(){
        $("div#debug_console div.hidden").slideToggle("fast");
    });
	
	if ($('#dialog .error').length) {
		$("#dialog .error").click(function(){
			$(this).slideUp('slow');
		});
	}
    
    if ($('div#content').length) {
			$('div#content').append('<div class="round_b"><div></div></div>');
            $('div#content').prepend('<div class="round_t"><div> </div></div>');
	}

	$("div#content tbody th").hover(function() {
		$('div', this).show();
	}, function() {
		$('div', this).hide();
	});
	
	screenshotPreview();
	imagePreview();

    
    $(".pane a.delete").click(function(){
        var name = $(this).attr("rel");
		if (confirm("Удалить категорию " + name + " ?")) 
        {
            var id = $(this).parents(".pane").attr("id");
            var action = $(this).attr("class");
            $.ajax({
                url: "/admin/category/go/",
                cache: false,
                type: "POST",
                data: "type=" + action + "&id=" + id + "&name=" + name,
                dataType: "text",
                beforeSend: function(){
                    $('div.loading').show();
                },
                complete: function(){
                    $('div.loading').hide();
                },
                success: function(resp){
                    $("div.success").show().html(resp);
                }
            });
            $(this).parents(".pane").fadeOut();
        }
        return false;
	});

    
    $(".catalog a.delete").click(function(){
        var name = $(this).prev().prev().attr("title");
        if (confirm("Удалить фотографию " + name + " ?")) 
        {
            var id = $(this).attr("id");
            var action = $(this).attr("class");
            
            $(this).fadeOut();
            $.ajax({
                url: "/admin/photo/go/",
                cache: false,
                type: "POST",
                data: "type=" + action + "&id=" + id + "&name=" + name,
                dataType: "text",
                beforeSend: function(){
                    $('div.loading').show();
                },
                complete: function(){
                    $('div.loading').hide();
                },
                success: function(resp){
                    $("div.success").show().html(resp);
                }
            });
        }
        return false;
	});
    
    var tabContainers = $('div.tabs > div');
    tabContainers.hide().filter(':first').show();
    
    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();   
});